import tkinter as tk


class Jisuanqi():
    def __init__(self):
        root = tk.Tk()
        root.title = "计算器"
        root.geometry("295x280+150+150")
        root.attributes('-alpha', 0.9)
        root['background'] = '#ffffff'
        self.root = root
        self.lists = []
        self.result_num = tk.StringVar()
        self.result_num.set(0)
        lable1 = tk.Label(root, textvariable=self.result_num, width=20, height=2, font=('宋体', 20), justify='left',
                          background='#ffffff', anchor='se')
        lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
        self.set_btn()

    def set_btn(self):
        root = self.root
        button_clear = tk.Button(root, text='C', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                 command=lambda: self.clear())
        button_back = tk.Button(root, text='←', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                command=lambda: self.back())
        button_division = tk.Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                    command=lambda: self.operator('/'))
        button_multiplication = tk.Button(root, text='x', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                          command=lambda: self.operator('*'))

        button_clear.grid(padx=4, row=1, column=0)
        button_back.grid(padx=4, row=1, column=1)
        button_division.grid(padx=4, row=1, column=2)
        button_multiplication.grid(padx=4, row=1, column=3)

        button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                 command=lambda: self.append_num('7'))
        button_eight = tk.Button(root, text='8', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                 command=lambda: self.append_num('8'))
        button_nine = tk.Button(root, text='9', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                command=lambda: self.append_num('9'))
        button_subtraction = tk.Button(root, text='—', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                       command=lambda: self.operator('-'))
        button_seven.grid(padx=4, row=2, column=0)
        button_eight.grid(padx=4, row=2, column=1)
        button_nine.grid(padx=4, row=2, column=2)
        button_subtraction.grid(padx=4, row=2, column=3)

        button_four = tk.Button(root, text='4', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                command=lambda: self.append_num('4'))
        button_four.grid(padx=4, pady=4, row=3, column=0)
        button_five = tk.Button(root, text='5', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                command=lambda: self.append_num('5'))
        button_five.grid(padx=4, row=3, column=1)
        button_six = tk.Button(root, text='6', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                               command=lambda: self.append_num('6'))
        button_six.grid(padx=4, row=3, column=2)
        button_addition = tk.Button(root, text='+', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0',
                                    command=lambda: self.operator('+'))
        button_addition.grid(padx=4, row=3, column=3)

        button_one = tk.Button(root, text='1', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                               command=lambda: self.append_num('1'))
        button_one.grid(padx=4, row=4, column=0)
        button_two = tk.Button(root, text='2', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                               command=lambda: self.append_num('2'))
        button_two.grid(padx=4, row=4, column=1)
        button_three = tk.Button(root, text='3', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                 command=lambda: self.append_num('3'))
        button_three.grid(padx=4, row=4, column=2)
        button_equal = tk.Button(root, text='=', width=5, height=3, font=('宋体', 16), relief='flat',
                                 background='#C0C0C0',
                                 command=lambda: self.equal())
        button_equal.grid(padx=4, row=4, rowspan=5, column=3)

        button_zero = tk.Button(root, text='0', width=12, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                command=lambda: self.append_num('0'))
        button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2)
        button_decimal = tk.Button(root, text='.', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD',
                                   command=lambda: self.append_num('.'))
        button_decimal.grid(padx=4, row=5, column=2)

    def run(self):
        # 进入循环显示
        self.root.mainloop()

    def append_num(self, i):
        self.lists.append(i)
        self.result_num.set(''.join(self.lists))

    def operator(self, i):
        if len(self.lists) > 0:
            if self.lists[-1] in ['+', '-', '*', '/']:
                self.lists[-1] = i
            else:
                self.lists.append(i)
            self.result_num.set(''.join(self.lists))

    def clear(self):
        self.lists.clear()
        self.result_num.set(0)

    def back(self):
        del self.lists[-1]
        self.result_num.set(self.lists)

    def equal(self):
        a = ''.join(self.lists)
        end_num = eval(a)
        self.result_num.set(end_num)
        self.lists.clear()
        self.lists.append(str(end_num))


if __name__ == '__main__':
    jisuanqi = Jisuanqi()
    jisuanqi.run()
